package org.dtrd.modules.drug.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.common.aspect.annotation.AutoLog;
import org.dtrd.modules.drug.entity.bean.DrugDetail;
import org.dtrd.modules.drug.entity.bean.DrugListInfo;
import org.dtrd.modules.drug.entity.request.DrugPageRequest;
import org.dtrd.modules.drug.service.IDtrdExtEmsYwDrugsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static org.dtrd.common.constant.CommonConstant.OPERATE_TYPE_DELETE;
import static org.dtrd.common.constant.CommonConstant.OPERATE_TYPE_QUERY;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_OPERATION_FAILURE;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_PARAMS_MISSING;

/**
 * <p>
 * 药品库 前端控制器
 * </p>
 *
 * @author QR
 * @since 2021-12-02
 */
@Api(tags = "药品库")
@RestController
@RequestMapping("/100/13/")
public class DrugController extends BaseController {

    @Autowired
    private IDtrdExtEmsYwDrugsService drugsService;

    @ApiOperation(value = "分页获取药品列表", notes = "支持模糊查询")
    @PostMapping("1001302")
    @AutoLog(value="分页获取药品列表", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getDrugPage(@RequestBody DrugPageRequest request){
        // 处理请求参数
//        if (isIntParamInvalid(request.getCategoryId())) {
//            return error(STATE_PARAMS_MISSING);
//        }
        Integer pageSize = getPageSize(request.getPageSize(), 20);
        request.setPageSize(pageSize);
        Integer page = getIntParam(request.getPage(), 1);
        request.setPage(page);
        IPage<DrugListInfo> drugPage = drugsService.getDrugPage(request);
        return Result.OK(drugPage);
    }

    @ApiOperation(value = "获取药物详情",response = DrugDetail.class)
    @GetMapping("/1001303")
    @AutoLog(value="获取药物详情", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getDrugTotalInfo(@RequestParam Integer drugId){
        if (isIntParamInvalid(drugId)) {
            return error(STATE_PARAMS_MISSING);
        }
        DrugDetail info = drugsService.getDrugs(drugId);
        return Result.OK(info);
    }

    @ApiOperation("删除药物")
    @DeleteMapping("1001304")
    @AutoLog(value="删除药物", operateType = OPERATE_TYPE_DELETE)
    public Result<?> removeDrug(@RequestParam Integer drugId){
        boolean result = drugsService.removeDrugs(drugId);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }

    @ApiOperation("添加或修改药品")
    @PostMapping("1001305")
    @AutoLog(value="添加或修改药品", operateType = OPERATE_TYPE_QUERY)
    public Result<?> saveOrUpdateDrugCategory(@RequestBody DrugDetail dto){
        if (isIntParamInvalid(dto.getDrugCategoryid())
                || StrUtil.isBlank(dto.getDrugName())) {
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = drugsService.saveOrUpdateDrug(dto);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }
}
